'use strict';
(function() {

    function initAuthorMap(authors){
        var authorMap = {};
        angular.forEach(authors, function(author, key){
            authorMap[author.name] = author;
        });
        return authorMap;
    }

    

    function initData(scope, state, authorMap){
        var authors = state.authors ? state.authors.split(",") : [];
        scope.authors = [];
        angular.forEach(authors, function(name, key){
            var author = authorMap[name];
            if(author){
                scope.authors.push(author);
            }
            
        });
        scope.progress = state.progress;
    }

    ngApp.directive("showcaseStatus", ['$rootScope', '$state', 'AuthorsConstant', function($rootScope, $state, AuthorsConstant) {
        return {
            'restrict': 'E',
            'replace': false,
            scope: {},
            templateUrl: "showcase/directive/showcase-status/view/showcase-status.html",
            link: function(scope, element, attrs) {
                var authorMap = initAuthorMap(AuthorsConstant);

                $rootScope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams) {
                    initData(scope, toState, authorMap);
                });

                initData(scope, $state.current, authorMap);
            }
        };
    }]);


})();